import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
/**
 * @Title: BillingCalculator.java
 * @Description: 计算机时制话费类
 * @author 买买提沙答木·托呼吐尔
 * @version V1.0
 * @Date 2023-3-22
 */
public class BillingCalculator {

    public double calculate(LocalDateTime startTime, LocalDateTime endTime) {
        double durationInMinutes = calculateDurationInMinutes(startTime, endTime);
        double cost = 0;

        if (durationInMinutes < 1) {
            cost = 0.05;
        } else if (durationInMinutes>=1&&durationInMinutes <= 20) {
            cost = durationInMinutes * 0.05;
        } else {
            cost = 1 + (durationInMinutes - 20) * 0.1;
        }

        return cost;
    }

    private double calculateDurationInMinutes(LocalDateTime startTime, LocalDateTime endTime) {
        ZonedDateTime startZonedDateTime = ZonedDateTime.of(startTime, ZoneId.systemDefault());
        ZonedDateTime endZonedDateTime = ZonedDateTime.of(endTime, ZoneId.systemDefault());

        // Handle daylight saving time
        if (startZonedDateTime.isBefore(endZonedDateTime)) {
            return startZonedDateTime.until(endZonedDateTime, java.time.temporal.ChronoUnit.MINUTES);
        } else {
            return endZonedDateTime.until(startZonedDateTime, java.time.temporal.ChronoUnit.MINUTES);
        }
    }
}
